package com.ruoyi.web.controller.minglvmini;

import com.github.pagehelper.PageInfo;
import com.minglv.mini.domain.CheeseLog;
import com.minglv.mini.domain.IntegralLog;
import com.minglv.mini.domain.User;
import com.minglv.mini.domain.UserInformation;
import com.minglv.mini.domain.dto.CheeseDto;
import com.minglv.mini.domain.dto.IntegralDto;
import com.minglv.mini.domain.dto.UserBo;
import com.minglv.mini.domain.dto.UserVo;
import com.minglv.mini.mapper.CheeseLogMapper;
import com.minglv.mini.mapper.IntegralLogMapper;
import com.minglv.mini.service.IUserInformationService;
import com.minglv.mini.service.IUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * 【请填写功能名称】Controller
 *
 * @author lxh
 * @date 2025-08-10
 */
@Api(tags = "Amini-用户管理")
@RestController
@RequestMapping("/system/mini/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @Resource
    private CheeseLogMapper cheeseLogMapper;

    @Resource
    private IntegralLogMapper integralLogMapper;

    @Resource
    private IUserInformationService userInformationService;

    @PostMapping("/editCheese")
    @ApiOperation("编辑奶酪数量")
    @Log(title = "用户管理-编辑奶酪数量", businessType = BusinessType.UPDATE)
    public AjaxResult postCheese(@RequestBody @Validated CheeseDto cheeseDto) {
        User user = userService.selectUserById(cheeseDto.getId());
        if (ObjectUtils.isNotEmpty(user)) {
            user.setCheese(user.getCheese().add(cheeseDto.getCheeseNum()));
            int i = userService.updateUser(user);
            if (i > 0) {
                CheeseLog cheeseLog = new CheeseLog();
                cheeseLog.setId(cheeseLogMapper.getMaxId() + 1);
                cheeseLog.setUserId(user.getId());
                cheeseLog.setCheese(cheeseDto.getCheeseNum());
                cheeseLog.setRemark("系统追加");
                LoginUser loginUser = SecurityUtils.getLoginUser();
                SysUser suser = loginUser.getUser();
                cheeseLog.setFromUser(suser.getUserId());
                cheeseLogMapper.insertCheeseLog(cheeseLog);
            }
            return toAjax(i);
        } else {
            return error("用户不存在");
        }
    }

    @PostMapping("/editIntegral")
    @ApiOperation("编辑水滴数量")
    @Log(title = "用户管理-编辑水滴数量", businessType = BusinessType.UPDATE)
    public AjaxResult postIntegral(@RequestBody @Validated IntegralDto integralDto) {
        User user = userService.selectUserById(integralDto.getId());
        if (ObjectUtils.isNotEmpty(user)) {
            user.setIntegral(user.getIntegral().add(integralDto.getIntegralNum()));
            int i = userService.updateUser(user);
            if (i > 0) {
                IntegralLog log = new IntegralLog();
                log.setId(integralLogMapper.getMaxId() + 1);
                log.setUserId(user.getId());
                log.setIntegral(integralDto.getIntegralNum());
                log.setRemark("系统追加");
                LoginUser loginUser = SecurityUtils.getLoginUser();
                SysUser suser = loginUser.getUser();
                log.setFromUser(suser.getUserId());
                integralLogMapper.insertIntegralLog(log);
            }
            return toAjax(i);
        } else {
            return error("用户不存在");
        }
    }
    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserBo user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(list).getTotal());
        if (!CollectionUtils.isEmpty(list)) {
            List<UserVo> userVos = new java.util.ArrayList<>();
            for (User user1 : list) {
                UserVo userVo = new UserVo();
                BeanUtils.copyProperties(user1, userVo);
                if (user.getIsInformation() != null && user.getIsInformation() == 1) {
                    UserInformation ui = new UserInformation();
                    ui.setUserId(user1.getId());
                    List<UserInformation> userInformations = userInformationService.selectUserInformationList(ui);
                    if (!CollectionUtils.isEmpty(userInformations)) {
                        userVo.setUserInformation(userInformations.get(0));
                    }
                }
                userVos.add(userVo);
            }
            rspData.setRows(userVos);
        }
        return rspData;
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:user:export')")
//    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, User user)
//    {
//        List<User> list = userService.selectUserList(user);
//        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
//        util.exportExcel(response, list, "【请填写功能名称】数据");
//    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }
}
